{% extends "base.html" %}

{% block title %}成绩管理{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-md-12">
            <h1 class="display-5">成绩管理</h1>
            <a href="{{ url_for('add_score') }}" class="btn btn-primary btn-sm float-end">
                <i class="fas fa-plus me-2"></i>添加成绩
            </a>
        </div>
    </div>
    
    <!-- 筛选条件 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <form method="get" action="{{ url_for('scores') }}" class="row g-3">
                        <div class="col-md-5">
                            <select name="student_id" class="form-select">
                                <option value="">所有学生</option>
                                {% for student in students %}
                                <option value="{{ student.id }}" {% if selected_student == student.id %}selected{% endif %}>
                                    {{ student.student_id }} - {{ student.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-5">
                            <select name="course_id" class="form-select">
                                <option value="">所有课程</option>
                                {% for course in courses %}
                                <option value="{{ course.id }}" {% if selected_course == course.id %}selected{% endif %}>
                                    {{ course.course_id }} - {{ course.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-filter me-2"></i>筛选
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 成绩表格 -->
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>学生</th>
                                    <th>课程</th>
                                    <th>成绩</th>
                                    <th>学分</th>
                                    <th>授课教师</th>
                                    <th>备注</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for score in scores.items %}
                                <tr>
                                    <td>{{ score.student.name }} ({{ score.student.student_id }})</td>
                                    <td>{{ score.course.name }} ({{ score.course.course_id }})</td>
                                    <td>
                                        {% if score.score >= 90 %}
                                        <span class="text-success font-weight-bold">{{ score.score }}</span>
                                        {% elif score.score >= 60 %}
                                        <span class="text-primary">{{ score.score }}</span>
                                        {% else %}
                                        <span class="text-danger font-weight-bold">{{ score.score }}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ score.course.credit }}</td>
                                    <td>{{ score.course.teacher }}</td>
                                    <td>{{ score.remark or '-' }}</td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ url_for('edit_score', id=score.id) }}" class="btn btn-sm btn-info">
                                                <i class="fas fa-edit me-1"></i>编辑
                                            </a>
                                            <form action="{{ url_for('delete_score', id=score.id) }}" method="post" 
                                                  onsubmit="return confirm('确定要删除该成绩记录吗？')">
                                                <button type="submit" class="btn btn-sm btn-danger">
                                                    <i class="fas fa-trash me-1"></i>删除
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td colspan="7" class="text-center">暂无成绩记录</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    
                    <!-- 分页导航 -->
                    <div class="mt-4 d-flex justify-content-between align-items-center">
                        <div class="text-muted">
                            显示第 {{ scores.page }} 页，共 {{ scores.pages }} 页，
                            总计 {{ scores.total }} 条记录
                        </div>
                        <nav aria-label="Page navigation">
                            <ul class="pagination justify-content-end">
                                <li class="page-item {% if not scores.has_prev %}disabled{% endif %}">
                                    <a class="page-link" href="{{ url_for('scores', page=scores.page-1, student_id=selected_student, course_id=selected_course) }}" 
                                       tabindex="-1">上一页</a>
                                </li>
                                {% for page_num in scores.iter_pages(left_edge=1, left_current=1, right_current=2, right_edge=1) %}
                                {% if page_num %}
                                <li class="page-item {% if page_num == scores.page %}active{% endif %}">
                                    <a class="page-link" href="{{ url_for('scores', page=page_num, student_id=selected_student, course_id=selected_course) }}">{{ page_num }}</a>
                                </li>
                                {% else %}
                                <li class="page-item disabled"><a class="page-link">...</a></li>
                                {% endif %}
                                {% endfor %}
                                <li class="page-item {% if not scores.has_next %}disabled{% endif %}">
                                    <a class="page-link" href="{{ url_for('scores', page=scores.page+1, student_id=selected_student, course_id=selected_course) }}">下一页</a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}